package com.ljl.rest;

import org.apache.activemq.broker.region.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import javax.jms.Topic;


/**
 * @author 刘家良
 * @date 2022/10/29 11:17
 * @description /
 */
@RestController
public class JmsController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @JmsListener(destination = "my.topic", containerFactory = "jmsListenerContainerTopic")
    private void topicMsg(String msg) {
        System.out.printf("收到topic消息[%s]\r\n", msg);
    }

    @JmsListener(destination = "my.queue", containerFactory = "jmsListenerContainerQueue")
    private void queueMsg(String msg) {
        System.out.printf("收到queue消息[%s]\n", msg);
    }

    @RequestMapping("/sendTopic/{msg}")
    public String sendTopicMsg(@PathVariable(name = "msg") String msg) {
        Destination destination = new ActiveMQTopic("my.topic");
        jmsMessagingTemplate.convertAndSend(destination , msg);
        return "ok";
    }

    @RequestMapping("/sendQueue/{msg}")
    public String sendQueueMsg(@PathVariable(name = "msg") String msg) {
        Destination destination = new ActiveMQQueue("my.queue");
        jmsMessagingTemplate.convertAndSend(destination , msg);
        return "ok";
    }

}
